using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ProgramMain.Framework;

namespace ProgramMain.UniversalTariffPlans
{
    public partial class UniversalTariffAproveCtl : BaseDetailCtl
    {
        public BindingSource UniversalTariffDataSource;
        private bool m_bOnActionClose = false;

        public UniversalTariffAproveCtl()
        {
            InitializeComponent();
        }

        public DialogResult ShowModal()
        {
            return frmBaseModal.ShowModal(this, labelCaption.Text, this.btnSave, this.btnCancel);
        }

        override public void FrmCommon_Load(object sender, EventArgs e)
        {
            if (this.ParentForm != null)
            {
                this.ParentForm.FormClosed += new FormClosedEventHandler(this.FrmCommon_FormClosed);
                this.ParentForm.FormClosing += new FormClosingEventHandler(this.FrmCommon_FormClosing);
            }

            if (this.ParentForm != null)
            {
                this.ParentForm.Text = this.labelCaption.Text;
            }

            this.BindDataControls();
        }

        private void BindDataControls()
        {
            this.dateEditStartDate.DataBindings.Add(new Binding("DateTime", this.UniversalTariffDataSource, "StartDate", true, DataSourceUpdateMode.OnPropertyChanged, null));
            this.dateEditEndDate.DataBindings.Add(new Binding("DateTime", this.UniversalTariffDataSource, "EndDate", true, DataSourceUpdateMode.OnPropertyChanged, null));
            this.checkIsAproved.DataBindings.Add(new Binding("Checked", this.UniversalTariffDataSource, "Is_Approved", true, DataSourceUpdateMode.OnPropertyChanged, false));

            dateEditStartDate.EditValue = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            dateEditEndDate.EditValue = new DateTime(2100, 01, 01);
        }

        private bool SaveData()
        {
            this.UniversalTariffDataSource.EndEdit();

            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            m_bOnActionClose = SaveData();

            if (m_bOnActionClose && this.ParentForm != null)
                this.ParentForm.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            m_bOnActionClose = true;

            this.UniversalTariffDataSource.CancelEdit();

            if (this.ParentForm != null)
                this.ParentForm.Close();
        }

        public void FrmCommon_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (!m_bOnActionClose)
            {
                m_bOnActionClose = true;

                this.UniversalTariffDataSource.CancelEdit();
            }
        }

        public void FrmCommon_FormClosing(object sender, FormClosingEventArgs e)
        {
            //if (this.ParentForm != null)
            {
                e.Cancel = false;
            }
        }

        private void btnChooseCurrentDate_Click(object sender, EventArgs e)
        {
            dateEditStartDate.EditValue = DateTime.Now.Date;
        }

        private void btnChooseEndDate_Click(object sender, EventArgs e)
        {
            dateEditEndDate.EditValue = new DateTime(2100, 01, 01);
        }
    }
}
